#include "stdafx.h"
#include "CMapp.h"
#include <fstream>
using namespace std;


CMapp::CMapp()
{
}


CMapp::~CMapp()
{
	
}

void CMapp::InitMap()
{
	LoadImg();
	ReadMaptxt();
	CreateBkImg();
	
}

void CMapp::LoadImg()
{
	char file[] = "./Map/map0.jpg";
	for (int i = 0; i < 3; i++)
	{
		sprintf_s(file, "./Map/map%d.jpg", i);
		m_BlockImg[i] = cvLoadImage(file,1);
	}
}

void CMapp::ReadMaptxt()
{
	fstream maptxt;
	maptxt.open("./Map/maptxt.txt", ios::in);
	for (int i = 0; i < 11; i++)
	{
		for (int j = 0; j < 11; j++)
		{
			maptxt >> m_map[i][j];
		}
	}
	maptxt.close();
}

void CMapp::ReleaseMap()
{
	for (int i = 0; i < 3; i++)
		cvReleaseImage(&m_BlockImg[i]);
	cvReleaseImage(&m_BackImg);
	
}

void CMapp::CreateBkImg()
{
	int blockr, blockc,px = 0,py = 0;
	m_BackImg = cvCreateImage(CvSize(11 * 60, 11 * 60), IPL_DEPTH_8U, 3);
	for (blockr = 0; blockr < 11; blockr++)
	{
		px = 0;
		for (blockc = 0; blockc < 11; blockc++)
		{
			if (m_map[blockr][blockc] != -1)
			{
				IplImage *pimg = m_BlockImg[m_map[blockr][blockc]];
				//IplImage *pimg = cvLoadImage("./Map/map0.jpg");
				for (int i = 0; i<pimg->width; i++)
				{
					for (int j = 0; j<pimg->height; j++)
					{
						CV_IMAGE_ELEM(m_BackImg, uchar, py + i, (px + j) * 3 + 0) = CV_IMAGE_ELEM(pimg, uchar, i, j * 3 + 0);
						CV_IMAGE_ELEM(m_BackImg, uchar, py + i, (px + j) * 3 + 1) = CV_IMAGE_ELEM(pimg, uchar, i, j * 3 + 1);
						CV_IMAGE_ELEM(m_BackImg, uchar, py + i, (px + j) * 3 + 2) = CV_IMAGE_ELEM(pimg, uchar, i, j * 3 + 2);
					}
				}
			}
			px += 60;
		}
		py += 60;
	}
}
